<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>过滤器</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.5/dayjs.min.js"></script>
</head>
<body>
    <div id="root">
        <h2>显示格式化后的时间</h2>
        <!--计算属性实现-->
        <h3>现在是：{{fmtTime}}</h3>
        <!--methods实现-->
        <h3>现在是：{{getFmtTime()}}</h3>
        <!--过滤器实现-->
        <h3>现在是：{{time | timeformater}}</h3>
         <!--过滤器实现(传参)-->
         <h3>现在是：{{time | timeformater('YYYY-MM-DD') | mySlice}}</h3>
    </div>
    <div id="root1">
        {{name | mySlice}}
    </div>
</body>
<script type="text/javascript">
    Vue.config.productionTip=false;
    //全局过滤器
    Vue.filter('mySlice',function(value){
        return value.slice(0,4)
    })

    new Vue({
        el:'#root',
        data:{
            time:1665798486869
        },
        computed: {
            fmtTime(){
                return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
            }
        },
        methods: {
            getFmtTime(){
                return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
            }
        },
        //局部过滤器
        filters: {
            timeformater(value,str='YYYY-MM-DD HH:mm:ss'){
                return dayjs(value).format(str)
            }
        }
    })

    new Vue({
        el:'#root1',
        data:{
            name:'sdsdsds'
        }
    })
</script>
</html>